using System;
using System.Linq;
using System.Collections.Generic; 

// This file is auto generated and will be overwritten as soon as the template is executed
// Do not modify this file...
	
namespace Stanford.Data
{   
	public partial class StudentRepository
	{
		private IRepository<Student> _repository {get;set;}
		public IRepository<Student> Repository
		{
			get { return _repository; }
			set { _repository = value; }
		}
		
		public StudentRepository(IRepository<Student> repository, IUnitOfWork unitOfWork)
    	{
    		Repository = repository;
			Repository.UnitOfWork = unitOfWork; 
    	}
		
		public IQueryable<Student> All()
		{
			return Repository.All();
		}

		/// <summary>
        /// Incluede de lay cac bang quan he
        /// </summary>
        /// <param name="includes">Danh sach chua ten cac bang quan he can lay cach nhau bang dau ,</param>
        /// <returns>query da lay nhung bang quan he</returns>
        /// <modified>
        /// Author              Date                comments
        /// AnhHn              25/07/2012          Tao moi
        /// </modified>
		public IQueryable<Student> All(string includes)
		{
			return Repository.All(includes);
		}
		
		/// <summary>
        /// Incluede de lay cac bang quan he
        /// </summary>
        /// <param name="lstInclude">Danh sach chua ten cac bang quan he can lay</param>
        /// <returns>query da lay nhung bang quan he</returns>
        /// <modified>
        /// Author              Date                comments
        /// AnhHn              25/07/2012          Tao moi
        /// </modified>
		public IQueryable<Student> All(List<string> lstInclude)
		{
			return Repository.All(lstInclude);
		}


      public Student GetById(int Id)
		{
		    try
			{
		         StanfordDatabaseEntities context = (StanfordDatabaseEntities)Repository.UnitOfWork.Context;
                 return context.Students.First(dk => dk.Id == Id);			
			 }
			 catch
			 {
			     return null;
			 }

		}

		//Ham them mot doi tuong sau do tra ve doi tuong duoc them do
		 public Student SaveReturnToObject(Student obj)
		{
		    try
			{
		         StanfordDatabaseEntities context = (StanfordDatabaseEntities)Repository.UnitOfWork.Context;

                 context.Students.AddObject(obj);	

				 context.SaveChanges();		
			 }
			catch(Exception ex) 
            {
                throw new Exception(ex.Message + ex.StackTrace + ex.Source);
			     return null;
			 }

			 return obj;
		}

		public void Add(Student entity)
		{
			Repository.Add(entity);
		}
		
		public void Delete(Student entity)
		{
			Repository.Delete(entity);
		}

		public void Save()
		{
			Repository.Save();
		}
	}
}